package com.haipumi.biz.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import com.haipumi.beans.haipumi_product;
import com.haipumi.beans.haipumi_taskType;
import com.haipumi.biz.TaskTypeBiz;
import com.haipumi.dao.DBHelper;

public class TaskTypeBizImpl implements TaskTypeBiz {
	private DBHelper db=new DBHelper();
	
	@Override
	public List<haipumi_taskType> findAllTaskType(String ttname) {
		List<Object> params=new ArrayList<Object>();
		String sql="select * from haipumi_tasktype";
		if(!ttname.equals("") && ttname!=null){
			sql="select * from haipumi_tasktype where ttname like ?";
			params.add("%"+ttname+"%");
		}
		List<haipumi_taskType> list=null;
		try {
			list=db.findAll(sql, params,haipumi_taskType.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return list;
	}

	@Override
	public int findTaskTypeCount(String ttname) {
		List<Object> params=new ArrayList<Object>();
		String sql="select count(*) from haipumi_tasktype";
		if(!ttname.equals("") && ttname!=null){
			sql="select * from haipumi_tasktype where ttname like ?";
			params.add("%"+ttname+"%");
		}
		int total=0;
		try {
			total=(int)db.findDouble(sql, params);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return total;
	}

	@Override
	public haipumi_taskType addTaskType(haipumi_taskType type) {
		String sql="insert into haipumi_tasktype(ttname,ttdescribe,ttstatus) values( ?, ? ,default )";
		List<Object> params=new ArrayList<Object>();
		params.add(type.getTtname());
		params.add(type.getTtdescribe());
		try {
			type.setTtid(  Integer.toString( db.insertWithKey(sql, params) ) );
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return type;
	}

	@Override
	public haipumi_taskType updateTaskType(haipumi_taskType type) {
		String sql="update haipumi_taskType set ttname=? ,ttdescribe=?,ttstatus=? where ttid=?" ;
		List<Object> params=new ArrayList<Object>();
		params.add(type.getTtname());
		params.add(type.getTtdescribe());
		params.add(type.getTtstatus());
		params.add(type.getTtid());
		try {
			db.doUpdate(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return type;
	}
	
	@Override
	public List<haipumi_taskType> findType(String ttname, int rows, int page, String sort,
			String order) {
		int start=(page-1)*rows;
		String sql="";//区分是否是按名字查询
		List<Object> params=new ArrayList<Object>();
		if("".equals(ttname)){
			sql="select * from haipumi_taskType order by ? ? limit "+start+","+rows;
			params.add(sort);
			params.add(order);
		}else{
			sql="select * from haipumi_taskType where ttname like ? order by ? ? limit "+start+","+rows;
			params.add("%"+ttname+"%");
			params.add(sort);
			params.add(order);
		}
		List<haipumi_taskType> list=null;
		try {
			list=db.findAll(sql, params, haipumi_taskType.class);
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new RuntimeException();
		}
		return list;
	}

	@Override
	public haipumi_taskType updateTypeStatus(haipumi_taskType type) {
		String sql="update haipumi_taskType set ttstatus=? where ttid =? ";
		List<Object> params=new ArrayList<Object>();
		params.add(type.getTtstatus());
		params.add(type.getTtid());
		try {
			db.doUpdate(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return type;
	}

	@Override
	public boolean deleteTaskType(String ttid) {
		String sql=" delete from haipumi_tasktype where ttid=? ";
		List<Object>paramsList=new ArrayList<Object>();
		paramsList.add(ttid);
		int result=0;
		try {
			result=db.doUpdate(sql, paramsList);
			if(result>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

}
